package com.example.yunhailutong.zhongtan.activity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.GongYingXuQiuEntity;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.squareup.picasso.Picasso;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yunhailutong on 2018/4/26.
 */

public class ActivityGongYingDesc extends ActivityBase {


    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.title2)
    TextView title2;
    @BindView(R.id.callman)
    TextView callman;
    @BindView(R.id.address)
    TextView address;

    @Override
    protected void addListener() {

    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        showWaitingDialog();
        Map<String, String> params = new HashMap<>();
//        params.put("username", phone.getText().toString());
//        params.put("password",password.getText().toString());
        params.put("id", getIntent().getStringExtra("id"));
        new PersionBiz().GetGongYingDesc(params);
    }

    @Override
    protected int contentViewById() {
        return R.layout.activity_xuqiu_desc;
    }


    @Subscribe
    public void log(OldsEvent.GongYingDescEvent msg) {
        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            GongYingXuQiuEntity entity = (GongYingXuQiuEntity) msg.obj;
            banner.setImageLoader(new GlideImageLoader());
            //设置图片集合
            banner.setImages(entity.getData().getImg());
            //banner设置方法全部调用完毕时最后调用
            banner.start();
            title.setText(entity.getData().getName());
            time.setText(entity.getData().getAddtime());
            title2.setText("公司名称："+entity.getData().getName());
            callman.setText("联系人："+entity.getData().getContent());
            address.setText("详细地址："+entity.getData().getFAddress());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //Picasso 加载图片简单用法
            String image = (String) path;
            String encode = null;
            try {
                encode = URLEncoder.encode(image, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String replace = encode.replace("%3A", ":").replace("%2F", "/");
            Picasso.with(context).load(replace).into(imageView);
        }
    }
}
